package com.kiln.juxingqs.ui.jy

import android.view.View
import client.global.commlib.comm_ui.draw.SpaceItemDecoration
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZcjlAdapter
import com.kiln.juxingqs.api.bean.Zcjl
import com.kiln.juxingqs.databinding.ActivityTicketRecordBinding
import com.kiln.juxingqs.ui.vm.ZrLotsViewModel

/**
 * 划转记录
 */
class TicketTransRecordActivity : BaseMiddleActivity<ZrLotsViewModel, ActivityTicketRecordBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_ticket_record
    }

    private var goods_id: String?=""

    val adapter: ZcjlAdapter by lazy {
        ZcjlAdapter(this, mImgLoader)
    }

    override fun initView() {
        binding?.apply {
            event = this@TicketTransRecordActivity
            vm = viewModel
            recyclerView.adapter = adapter
            recyclerView.addItemDecoration(SpaceItemDecoration(10))
            emptyLayout.setOnClickListener { viewModel.jYzcjl(goods_id!!,20) }
            refreshLayout.setOnRefreshListener {
                adapter.hasMoreState.value = true
                viewModel.pageOne.value = 0
                viewModel.jYzcjl(goods_id!!,20)
            }
            recyclerView.onLoadMore = {
                if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                    viewModel.jYzcjl(goods_id!!,20)
                }
            }
        }
    }

    override fun observerUI() {
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.retOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.record_list as MutableList<Zcjl.Record?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

    }

    override fun initData() {
        goods_id = intent.getStringExtra("goods_id")
        viewModel.pageOne.value = 0
        viewModel.jYzcjl(goods_id!!, 20)
    }

    override fun initViewModel(): ZrLotsViewModel = createViewModel()

}